const spawn = require("cross-spawn");
const path = require("path");
const fse = require("fs-extra");
exports.execCMD = (command = "", param = [], option = {}) => {
	const mergeOption = Object.assign(
		{},
		{
			encoding: "utf-8",
		},
		option,
	);
	let execResult = spawn.sync(command, param, mergeOption);

	let { status, error, output, stdout, stderr } = execResult;
	console.log(execResult);
	if (Object.is(status, 0)) {
		if (stderr) {
			throw stderr;
		}
		return {
			status,
			output,
			stdout,
		};
	} else {
		throw stdout || error;
	}
};

exports.getIdePort = () => {
	const localAppPath = process.env.LOCALAPPDATA;
	const idePath = path.join(
		localAppPath,
		"/微信web开发者工具/User Data/Default/.ide",
	);
	return fse.readFileSync(idePath);
};

exports.isInstallWXIde = () => {
	const localAppPath = process.env.LOCALAPPDATA;
	const idePath = path.join(
		localAppPath,
		"/微信web开发者工具/User Data/Default/.ide",
	);
	try {
		fse.accessSync(idePath, fse.constants.F_OK);
		return "微信开发者工具已经安装";
	} catch (error) {
		return error;
	}
};
